package com.iss.okair.service.trans;

import com.iss.common.UuidTools;
import com.iss.okair.db.entity.AssetsPermanent;
import com.iss.okair.db.entity.AssetsTranUse;
import com.iss.okair.db.entity.Assetstrans;
import com.iss.okair.db.mapper.AssetsPermanentMapper;
import com.iss.okair.db.mapper.CategoryMapper;
import com.iss.okair.db.mapper.PermanentTransMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.List;

@Service
public class PermanentTransService {

    @Resource
    private PermanentTransMapper mapper;
    @Resource
    private AssetsPermanentMapper pmapper;
    @Resource
    private CategoryMapper cmapper;

    public List<AssetsTranUse> find(AssetsTranUse model){
        return mapper.select(model);
    }
    public List<AssetsTranUse> findout(AssetsTranUse model){
        return mapper.selectout(model);
    }
    public List<AssetsTranUse> findrecord(String model){
        return mapper.selectrecord(model);
    }
    //入库
    public boolean add(Assetstrans trans){
        //设置id值
        trans.setId(UuidTools.buildUUID());
        //设置batchno的值
        //获取前端传入的资产的id值
        String asid = trans.getAssetsid();
        //将trans里的assetsid值传给buildBathno，获得符合格式的字符串
        String str = this.buildBathno(asid);
        //将符合格式的字符串赋值给trans中的batchno属性
        trans.setBatchno((str));
        trans.setTranstype("1");
        int temp = mapper.add(trans);
        return temp>=1?true:false;
    }

    //创建批次编号的方法String.format("%s-%s"，资产code值，当前系统时间的毫秒数)
    private String buildBathno(String id){
        //调用normal的mapper查询code值
        AssetsPermanent permanent = pmapper.selectById(id);
        String code = permanent.getAssetscode();
        //获取系统的时间
        Calendar cal = Calendar.getInstance();
        String time = cal.getTimeInMillis()+"";
        return String.format("%s-%s",code,time);
    }
    //领用和转移操作
    public boolean Use(String type,Assetstrans finance){
        //领用动作,出库

        if ("2".equals(type)){
            finance.setTranstype(type);
            return  mapper.add(finance)>=1 ? true:false;
        }else if("4".equals(type)) {
            //转移动作
            finance.setTranstype(type);
            return mapper.add(finance)>=1 ? true:false;
        }else {
            return false;
        }
    }
}
